package com.juno.cafenoa.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.juno.cafenoa.Constants;

/**
 * 어플에서 사용할 Preference 저장/불러오기 함수가 정의된곳.
 * @author JUNHO
 */
public class CafeNoaPreference{
	
	/**
	 * db version을 set함.
	 * @param context
	 * @param id
	 */
	public static void setDBVersion(Context context, String ver){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(Constants.CAFENOA_MENU_DB, ver);
		edit.commit();
	}
	
	/**
	 * db version을 가져옴
	 * @param context
	 * @return
	 */
	public static String getDBVersion(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String version =preferences.getString(Constants.CAFENOA_MENU_DB, ""); 
		if(version.equalsIgnoreCase(""))
			return "100";
		else
			return version; 
	}	
	
	/**
	 * gcm id를 프리퍼런스 상에 저장ㅇ함.
	 * @param context
	 * @param id
	 */
	public static void setGcmID(Context context, String id){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(Constants.GCM_SERVICE_REGIST, id);
		edit.commit();
	}
	
	/**
	 * gcm id를 꺼내옴.
	 * @param context
	 * @return
	 */
	public static String getGcmID(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getString(Constants.GCM_SERVICE_REGIST, "");
	}	
	
	
	public static void setRegistState(Context context, boolean flag){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putBoolean(Constants.CAFE_REGIST_STATE, flag);
		edit.commit();
	}
	
	public static boolean getRegistState(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getBoolean(Constants.CAFE_REGIST_STATE, false);
	}	
	
	/**
	 * 유저의 등급을 프리퍼런스상에 저장해둠.
	 * @param context
	 * @param newPoint
	 */
	public static void setUserGrade(Context context, String grade){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(Constants.USER_GRADE, grade);
		edit.commit();
	}
	
	/**
	 * 유저의 등급을 가져옴.
	 * @param context
	 * @return
	 */
	public static String getUserGrade(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getString(Constants.USER_GRADE,"");
	}
	
	
	/**
	 * 유저의 포인트를 프리퍼런스상에 더한후 저장해둠.
	 * @param context
	 * @param newPoint
	 */
	public static void setUserPoint(Context context, int newPoint){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		int point = preferences.getInt(Constants.USER_POINT, 0);
		Editor edit = preferences.edit();
		edit.putInt(Constants.USER_POINT, point + newPoint);
		edit.commit();
	}
	
	/**
	 * 유저의 포인트를 가져옴.
	 * @param context
	 * @return
	 */
	public static int getUserPoint(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getInt(Constants.USER_POINT, 0);
	}
	/**
	 * user password 설정
	 * @param context
	 * @param userId
	 */
	public static void setUserPassword(Context context, String userPassword){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(Constants.USER_PASSWORD, userPassword);
		edit.commit();
	}
	/**
	 * user password 가져옴
	 * @param context
	 * @return
	 */
	public static String getUserPassword(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String password = preferences.getString(Constants.USER_PASSWORD, "");
		return password;
	}
	
	/**
	 * user ID 설정
	 * @param context
	 * @param userId
	 */
	public static void setUserID(Context context, String userId){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(Constants.USER_ID, userId);
		edit.commit();
	}
	/**
	 * user ID 가져옴
	 * @param context
	 * @return
	 */
	public static String getUserID(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String ID = preferences.getString(Constants.USER_ID, "");
		return ID;
	}
	
	
	/**
	 * push를 받는지 안바는지 설정값 설정.
	 * @param context
	 * @param flag
	 */
	public static void setPushGet(Context context, boolean flag){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putBoolean(Constants.GET_PUSH_ITEM, flag);
		edit.commit();
	}
	
	/**
	 * push를 받는지 안받는지 설정값 가져옴.
	 * @param context
	 * @return
	 */
	public static boolean getPushGet(Context context){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getBoolean(Constants.GET_PUSH_ITEM, false);
	}
}
